package com.zte.purchase.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zte.login.pojo.Users;
import com.zte.login.service.UserService;
import com.zte.market.pojo.City;
import com.zte.market.pojo.Custom;
import com.zte.market.service.CityService;
import com.zte.market.service.ProvinceService;
import com.zte.purchase.dao.IPurchaseDao;
import com.zte.purchase.pojo.Purchase;
import com.zte.purchase.pojo.PurchaseCBrand;
import com.zte.purchase.pojo.PurchaseCFirm;
import com.zte.purchase.service.IPurchaseService;
import com.zte.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author luode
 * @version 1.0
 * @date 2022/10/02 星期日 17:43
 */
@Service
public class PurchaseImpl implements IPurchaseService {

    @Autowired
    private IPurchaseDao iPurchaseDao;

    @Autowired
    private ProvinceService provinceService;

    @Autowired
    private CityService cityService;

    @Autowired
    private UserService userService;
    /**
     * 查询采购部管理职位
     * @param job_dept_id 部门id
     * @return
     */
    @Override
    public ArrayList selectJobname(int job_dept_id) {
        return iPurchaseDao.selectJobname(job_dept_id);
    }

    /**
     * 查找采购部管理人员名称
     * @param job_name
     * @return
     */
    @Override
    public ArrayList selectUname(String job_name) {
        return iPurchaseDao.selectUname(job_name);
    }

    /**
     * 查找品牌信息
     * @param brand
     * @return
     */
    @Override
    public PageModel<PurchaseCBrand> selectbrand(PurchaseCBrand brand,PageModel<PurchaseCBrand> pageModel){
        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?3:pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(),pageModel.getPageSize());
        List<PurchaseCBrand> brandList = iPurchaseDao.selectbrand(brand);
        PageInfo<PurchaseCBrand> pageInfo = new PageInfo<PurchaseCBrand>(brandList);

        pageModel.setData(new ArrayList<PurchaseCBrand>(brandList));
        pageModel.setTotalPage(pageInfo.getPages());
        pageModel.setTotalRows(pageInfo.getTotal());
        return pageModel;
    }

    /**
     * 点击恢复/注销改变品牌状态
     * @param brand
     */
    @Override
    public int updateBrandStatus(PurchaseCBrand brand){
        int i = iPurchaseDao.updateBrandStatus(brand);
        return i;
    }

    /**
     * 增加品牌
     * @param brand
     * @return
     */
    public int addBrand(PurchaseCBrand brand){
        int i = iPurchaseDao.addBrand(brand);
        return i;
    }


    /**
     * 跳转修改品牌页面
     * @param brand
     * @return
     */
    @Override
    public PurchaseCBrand selectbrandOne(PurchaseCBrand brand) {
        PurchaseCBrand purchaseCBrand = iPurchaseDao.selectbrandOne(brand);
        return purchaseCBrand;
    }

    /**
     * 修改某一品牌
     * @param brand
     * @return
     */
    public int updateBrand(PurchaseCBrand brand){
        int i = iPurchaseDao.updateBrand(brand);
        return i;
    };

    /**
     * 查找厂商信息
     * @param purchaseCFirm
     * @param cityList
     * @param pageSize
     * @param pageNum
     * @return
     */
    public PageModel<PurchaseCFirm> queryByParam(PurchaseCFirm purchaseCFirm, List<City> cityList, Integer pageSize, Integer pageNum){
        List<Integer> cityIds = new ArrayList<>();
        if(cityList != null){
            for (int i = 0; i < cityList.size(); i++) {
                cityIds.add(cityList.get(i).getId());
            }
        }
        //是否分配，表示有没有服务人员id，-1表示未选择是否分配，1表示已分配，0表示未分配
        List<PurchaseCFirm> firmList = iPurchaseDao.queryByParam(purchaseCFirm,cityIds);
        for (int i=0;i<firmList.size();i++) {
            Integer createid =  firmList.get(i).getCreateId();
            Integer firmCity = firmList.get(i).getFirmCity();
            //向客户信息中添加 公司所在的地址信息
            if(firmCity != null){
                City city = cityService.queryById(firmCity);
                firmList.get(i).setCity(city);
            }
            //向客户信息中添加 创建人员信息
            if(createid != null){
                Users createUsers = userService.getUserById(createid);
                firmList.get(i).setCreateUsers(createUsers);
            }
        }
        PageModel<PurchaseCFirm> firmPage = queryByPage(firmList, pageSize, pageNum);
        return firmPage;
    }

    public PageModel<PurchaseCFirm> queryByPage(List<PurchaseCFirm> firmList,int pageSize,int pageNum) {

        PageModel<PurchaseCFirm> firmPage = new PageModel<>();
        firmPage.setPageNum(pageNum);//设置当前页
        firmPage.setPageSize(pageSize);//设置当前页的尺寸
        //总页数
        int totalPage = firmList.size()/pageSize;
        if(firmList.size()%pageSize != 0){
            totalPage += 1;
        }
        firmPage.setTotalPage(totalPage); //设置总页数
        firmPage.setTotalRows(firmList.size());//设置总记录数

        List<PurchaseCFirm> pageList = new ArrayList<>();
        int start = (pageNum-1)*pageSize;
        int end = pageNum*pageSize;//当页数据中最后一条数据对应的索引
        if(firmList.size()<end){
            end = firmList.size();
        }
        for(int i=start;i<end;i++){
            pageList.add(firmList.get(i));
        }

        firmPage.setData(pageList);//设置存储数据
        return firmPage;
    }



    /**
     * 获取所有客户信息,并向数据中添加相关类
     * @return public List<PurchaseCFirm>
     */
    public List<PurchaseCFirm> getAllFirm(){
        List<PurchaseCFirm> firmList = iPurchaseDao.queryAll();
        for (int i=0;i<firmList.size();i++) {
            Integer createId =  firmList.get(i).getCreateId();
            Integer firmCity = firmList.get(i).getFirmCity();
            //向客户信息中添加 公司所在的地址信息
            if(firmCity != null){
                City city = cityService.queryById(firmCity);
                firmList.get(i).setCity(city);
            }
            //向客户信息中添加 创建人员信息
            if(createId != null){
                Users  createUsers = userService.getUserById(createId);
                firmList.get(i).setCreateUsers(createUsers);
            }
        }
        return firmList;
    }
}
